package com.gitee.francis.lw.form.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gitee.francis.lw.form.model.request.FormItemRequestDTO;
import com.gitee.francis.lw.form.model.response.FormItemGetResponseDTO;
import com.gitee.francis.lw.form.model.response.FormItemListResponseDTO;
import com.gitee.francis.lw.form.model.search.FormItemSearchDTO;
import com.gitee.francis.lw.form.service.IControlService;
import com.gitee.francis.lw.form.service.IFormItemService;
import com.gitee.francis.lw.mapper.model.db.FormItemDO;
import com.hzlemone.controller.AbstractController;
import com.hzlemone.enums.ResponseStatusEnum;
import com.hzlemone.response.ResponseDTO;

/**
 * @ClassName ElseClassifyController.java
 * @Description 其他分类 REST API
 * @author Francis.LW
 * @date 2018年1月5日
 **/
@RestController
@RequestMapping("/admin/form/item")
@Api(value = "表单项 REST API", description = "表单项 REST API")
public class FormItemController
		extends
		AbstractController<FormItemDO, FormItemDO, FormItemSearchDTO, FormItemListResponseDTO, FormItemRequestDTO, FormItemGetResponseDTO> {

	@Autowired
	IFormItemService formItemService;

	@ApiOperation(value = "获取表单项列表", notes = "获取表单项列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "currentpage", value = "当前页数", defaultValue = "1", paramType = "query", required = true, dataType = "int"),
			@ApiImplicitParam(name = "pagesize", value = "每页记录数", defaultValue = "10", paramType = "query", required = true, dataType = "int") })
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@Override
	public ResponseDTO list(
			@RequestParam(name = "currentpage", defaultValue = "1") int currentPage,
			@RequestParam(name = "pagesize", defaultValue = "10") int pageSize,
			@RequestBody(required = false) FormItemSearchDTO search) {
		return super.list(currentPage, pageSize, search);
	}

	@ApiOperation(value = "添加表单项", notes = "添加表单项")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@Override
	public ResponseDTO save(@Valid @RequestBody FormItemRequestDTO requestDTO,
			BindingResult errorResult) {
		requestDTO.setCreatedTime(new Date());
		ResponseDTO responseDTO = new ResponseDTO();

		// 验证表单填写的信息
		List<String> errorTag = requestDTO.getFormError(errorResult);
		if (errorTag.size() != 0) {
			responseDTO.setCode(ResponseStatusEnum.FORM_ERROR.getCode());
			responseDTO.setMessage(ResponseStatusEnum.FORM_ERROR.getMessage());
			responseDTO.setData(errorTag);
			return responseDTO;
		}

		try {
			formItemService.saveFormItem(requestDTO);
			responseDTO.setCode(ResponseStatusEnum.SUCCESS.getCode());
			responseDTO.setMessage(ResponseStatusEnum.SUCCESS.getMessage());
		} catch (Exception ex) {
			logger.error("信息插入信息报错如下：" + ex.toString());
			responseDTO.setCode(ResponseStatusEnum.ERROR.getCode());
			responseDTO.setMessage(ResponseStatusEnum.ERROR.getMessage());
		}
		return responseDTO;
	}

	@ApiOperation(value = "获取表单项详情", notes = "获取表单项详情")
	@ApiImplicitParam(name = "id", value = "信息id", paramType = "path", required = true, dataType = "String")
	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	@Override
	public ResponseDTO getById(@PathVariable("id") String id) {
		return super.getById(id);
	}

	@ApiOperation(value = "编辑表单项", notes = "编辑表单项")
	@ApiImplicitParam(name = "id", value = "信息id", paramType = "path", required = true, dataType = "String")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
	@Override
	public ResponseDTO update(@PathVariable("id") String id,
			@Valid @RequestBody FormItemRequestDTO requestDTO,
			BindingResult errorResult) {
		ResponseDTO responseDTO = new ResponseDTO();

		// 验证表单填写的信息
		List<String> errorTag = requestDTO.getFormError(errorResult);
		if (errorTag.size() != 0) {
			responseDTO.setCode(ResponseStatusEnum.FORM_ERROR.getCode());
			responseDTO.setMessage(ResponseStatusEnum.FORM_ERROR.getMessage());
			responseDTO.setData(errorTag);
			return responseDTO;
		}

		try {
			formItemService.updateFormItem(id, requestDTO);
			responseDTO.setCode(ResponseStatusEnum.SUCCESS.getCode());
			responseDTO.setMessage(ResponseStatusEnum.SUCCESS.getMessage());
		} catch (Exception ex) {
			logger.error("信息插入信息报错如下：" + ex.toString());
			responseDTO.setCode(ResponseStatusEnum.ERROR.getCode());
			responseDTO.setMessage(ResponseStatusEnum.ERROR.getMessage());
		}
		return responseDTO;
	}

	@ApiOperation(value = "删除表单项", notes = "删除表单项，可进行单条或批量删除")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@Override
	public ResponseDTO remove(@RequestBody String[] ids) {
		return super.remove(ids);
	}
}
